import {
  columns,
  tabArr
} from './index.const';
import ElEditable from '@components/el-editable';
import {
  fileDownload
} from '@const/utils.js';
import {
  $loadFileSimply
} from '@const/loadFileOps.js';

import {
  findPaymentByOrderId,
  exportExcel,
  findPayReceivePaymentRemind,
  findPayTypeCount,
  pageList,
  findPayeeName,
  payBillManageSelectOrderTypeList
} from '@api/m_mall_web_v1.js';
import
tabs
from '@components/tabs/index.vue';
export default {
  components: {
    ElEditable,
    tabs
  },
  inject: ['apiMMallWeb'],
  data() {
    return {
      collapse: true,
      loading: false,
      options: [],
      // rules: {
      //   rule: [{
      //     required: true,
      //     message: '请填写退款原因',
      //     trigger: 'blur'
      //   }]
      // },
      // formData: {
      //   rule: '',
      //   price: ''
      // },
      tableQuery: {
        isDelayPay: '',
        orderCode: '',
        paymentStatus: null,
        payUse: '',
        busModel: '',
        orderType: '',
        payeeName: '',
        // remittanceOrderNo: '',
      },
      paymentDate: ['', ''],
      orderDate: ['', ''],
      tableLoading: false,
      tabArr: tabArr,
      columns: columns,
      delDialogVisible: false,
      payload: {},
      // 主列表分页
      currentPage: 1,
      pageSize: 20,
      height_collapse: false,
      typeList: [], //获取订单类型
    };
  },
  created() {
    this.getList()
    this.getFindPayTypeCount()
    // 获取订单类型数据
    this.getOrdertype()
    // this.getMessageList()
  },
  mounted() {
    if (this.$refs.queryForm.$el.clientHeight > 90) {
      this.height_collapse = true
      this.collapse = false
    }
  },
  methods: {
    //获取支付成功消息提醒
    // getMessageList() {
    //   this.$axios.$post(findPayReceivePaymentRemind)
    //     .then(res => {
    //       if (res.code === '0' && res.payload !== []) {
    //         res.payload.forEach(item => {
    //           setTimeout(() => {
    //             this.$message({
    //               dangerouslyUseHTMLString: true,
    //               center: true,
    //               type: "success",
    //               // offset: 50,
    //               message: `<div> <p >${item.remindType===1?'支付成功':'到账成功'}</p><p>${item.payeeName}</p> </div>`
    //             });
    //           }, 50)
    //         })
    //       }
    //     })
    // },
    //收款方下拉搜索
    remoteMethod(val) {
      if (val !== '') {
        this.loading = true;
        this.$axios.$get(`${findPayeeName}?payeeName=${val}`).then(res => {
          this.options = res.payload
          this.options = res.payload.map(item => {
            return {
              value: item,
              label: item
            }
          })
          this.loading = false;
        });
      }

    },
    //获取查询支付类型的数量
    getFindPayTypeCount() {
      this.$axios.$post(findPayTypeCount).then(res => {
        if (res.code === '0') {
          this.tabArr[1].badge.value = Number(res.payload.awaitPaymentCount);
          this.tabArr[2].badge.value = Number(res.payload.paidCount);
        }
      });
    },
    // 点击订单编号，去往详情
    toDetail(row) {
      if (row.busModel === '1') {
        this.$router.push({
          path: '/purchase-order/wholesale/list/detail',
          query: {
            id: row.orderId,
            orderDetailId: row.orderDetailId
          }
        });
      }
      if (row.busModel === '2') {
        this.$router.push({
          path: '/purchase-order/consign/list/detail',
          query: {
            id: row.orderId,
            orderDetailId: row.orderDetailId
          }
        });
      }
      if (row.busModel === '11') {
        this.$router.push({
          path: '/purchase-order/salesorder/list/detail',
          query: {
            id: row.orderId,
            orderDetailId: row.orderDetailId
          }
        });
      }
    },
    // tab切换
    handletabs(index) {
      if (index === 0) {
        this.tableQuery.paymentStatus = null;
      } else {
        this.tabArr.forEach((item, i) => {
          if (i === index) {
            this.tableQuery.paymentStatus = item.val;
          }
        });
      }
      this.queryFormQuery();
    },
    // 重置
    queryFormReset() {
      this.tableQuery = {
        isDelayPay: '',
        orderCode: '',
        payUse: '',
        paymentStatus: this.tableQuery.paymentStatus,
        busModel: '',
        payeeName: '',
        orderType: ''
        // remittanceOrderNo: '',
      }
      this.orderDate = ['', '']
      this.paymentDate = ['', '']
      this.currentPage = 1;
      this.getList();
    },
    // 查询
    queryFormQuery() {
      this.currentPage = 1
      this.getList()
    },
    async getList() {
      this.tableLoading = true;
      const query = {
        ...this.tableQuery,
        page: this.currentPage,
        pageSize: this.pageSize,
        orderDateStart: this.orderDate ? this.orderDate[0] : '',
        orderDateEnd: this.orderDate ? this.orderDate[1] : '',
        paymentDateStart: this.paymentDate ? this.paymentDate[0] : '',
        paymentDateEnd: this.paymentDate ? this.paymentDate[1] : '',
      };
      if (query.orderType === '') {
        query.orderType = null
      }
      this.$axios.$post(pageList, query).then(res => {
        if (res.code === '0') {
          this.payload = res.payload;
        }
        this.tableLoading = false
      })
    },
    // 主列表分页器
    reloadProductData(val) {
      this.currentPage = val.number
      this.pageSize = val.size
      this.getList();
    },
    // 导出
    exportEx() {
      if (this.payload.content && this.payload.content.length > 0) {
        const query = {
          ...this.tableQuery,
          page: this.currentPage,
          pageSize: this.pageSize,
          orderDateStart: this.orderDate ? this.orderDate[0] : '',
          orderDateEnd: this.orderDate ? this.orderDate[1] : '',
          paymentDateStart: this.paymentDate ? this.paymentDate[0] : '',
          paymentDateEnd: this.paymentDate ? this.paymentDate[1] : '',
        };
        $loadFileSimply({
            method: 'post',
            url: exportExcel,
            timeout: '0',
            data: {
              ...query
            }
          }).then(res => {
            if (res.byteLength) {
              fileDownload(res, '支付单.xls')
            } else {
              this.$message({
                message: '导出失败',
                type: 'warning'
              })
            }
          })
          .catch(err => {
            this.$message({
              message: '导出失败',
              type: 'warning'
            })
          })
      } else {
        this.$message({
          message: '暂无可导出数据',
          type: 'warning'
        })
      }
    },
    //获取订单类型
    getOrdertype() {
      // const params = {
      //   busModel: 1,
      // }
      this.$axios.$post(payBillManageSelectOrderTypeList, {})
      .then(res => {
          this.typeList = res.payload || {}
      })
    },
    // 退款弹窗
    // refund(scope) {
    //   this.delDialogVisible = true;
    //   this.orderIds = scope.orderId;
    // },
    // delSure() {
    //   this.$refs[ruleForm].validate((valid) => {
    //     if (valid) {
    //       const data = this.apiMMallWeb.DeleteOrderOrderV1ApiPOST({
    //         orderIds: this.orderIds
    //       });
    //       if (data === true) {
    //         this.$message.success('删除成功')
    //         this.getList();
    //       } else {
    //         this.$message.warning('删除失败')
    //       }
    //       this.delDialogVisible = false;
    //     }
    //   })
    // },
    // 关闭退款弹窗
    // concelDel() {
    //   this.delDialogVisible = false;
    // },
    //查看
    // checkDetail(scope) {
    //   this.$router.push({
    //     path: '/financial-management/accounts-payable-managemen/detail',
    //     query: {
    //       orderId: scope.orderId,
    //       orderStatus: scope.orderStatus
    //     }
    //   });
    // },
    // 去支付
    // toPay(scope) {
    //   this.$axios.$get(`${findPaymentByOrderId}?orderId=${scope.orderId}`).then(res => {
    //     if (res.payload.paymentStatus === '1') {
    //       this.$router.push({
    //         path: '/financial-management/accounts-payable-managemen/pay',
    //         query: {
    //           id: scope.orderId
    //         }
    //       });
    //     } else {
    //       this.$message.error('只有待支付状态才可以去支付')
    //       this.getList()
    //     }
    //   })

    // }
  }
};